<?php
/**
 * Eren Framework
 * 
 * Opreate String
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_String
{
    /**
     * Count the char of String
     * 
     * @param string $string
     * @param string $charset
     * @throws Eren_Exception
     * @return int
     */
    public static function count($string, $charset = 'utf-8')
    {
        if(!is_string($string) || !is_string($charset)){
            throw new Eren_Exception("The params of Eren_String::count must be string");
        }
        if(function_exists('mb_strlen')){
            return mb_strlen($string, $charset);
        }
        return strlen($string);
    }
    
    /**
     * Cut the String
     * 
     * @param string $string
     * @param int $limit
     * @param string $charset
     * @throws Eren_Exception
     * @return string
     */
    public static function cut($string, $limit, $etc = '...', $charset = 'utf-8')
    {
        if(!is_string($string)){
            throw new Eren_Exception("The first param of Eren_String::cut must be string");
        }
        if(!is_numeric($limit)){
            throw new Eren_Exception("The second param of Eren_String::cut must be int");
        }
        if(!is_string($charset)){
            throw new Eren_Exception("The third param of Eren_String::cut must be string");
        }
        if(function_exists('mb_substr')){
            return mb_substr($string, 0, $limit, $charset). $etc;
        }
        return substr($string, 0, $limit) . $etc;
    }
    
    /**
     * Convert Charact
     * 
     * @param string $inCharset
     * @param string $outCharset
     * @param string $string
     * @throws Eren_Exception
     * @return string
     */
    public static function convChar($inCharset, $outCharset, $string)
    {
        if(!is_string($inCharset) || !is_string($outCharset) || !is_string($string)){
            throw new Eren_Exception("The params of Eren_String::convChar must be string");
        }
        return mb_convert_encoding($string, $outCharset, $inCharset);
    }
    
    public static function convToChar($outCharset, $string)
    {
        if(!is_string($string)){
            throw new Eren_Exception("The params of Eren_String::convToUTF8 must be string");
        }
        $fileType = mb_detect_encoding($string , array('UTF-8','GBK','LATIN1','BIG5')) ;
        if( $fileType != $outCharset){
            $string = self::convChar($fileType, $outCharset, $string);
        }
        return $string;
    }
    
     /**
     * Convert to string and trim "\r\n\t " 
     * 
     * @param string $str
     * @throws Eren_Exception
     * @return string
     */
    public static function convToStr($str)
    {
        if(!is_string($str) && !is_int($str)){
            throw new Eren_Exception("The params of Eren_String::convToStr must be string or int");
        }
        return trim(strval($str), "\r\n\t ");
    }
    
     /**
     * Convert to int 
     * 
     * @param string $str
     * @throws Eren_Exception
     * @return int
     */
    public static function convToInt($str)
    {
        if(!is_string($str) && !is_int($str)){
            throw new Eren_Exception("The params of Eren_String::convToInt must be string or int");
        }
        return intval($str);
    }
}